<template>
  <div class="chart1">
    <PublicEcharts
      v-if="!loadingFlag"
      :title="'设备概况'"
      :option="this.indexDeviceData"
      :id="'indexDeviceData'"
      :method="this.chartDeviceClick"
    ></PublicEcharts>
    <div v-if="loadingFlag" class="e-loading">
      <dv-loading style="color: #fff;opcity:0.5">设备概况数据加载中...</dv-loading>
    </div>
  </div>
</template>

<script>
import { getDeviceOption } from "../../utils/chartOption/indexChartOption";
import PublicEcharts from "../../components/charts/PublicEcharts";

export default {
  //import引入的组件需要注入到对象中才能使用
  components: { PublicEcharts },
  props: {},
  data() {
    //这里存放数据
    return {
      indexDeviceData: null,
      loadingFlag: true,
      devCodes: []
    };
  },
  //计算属性 类似于data概念
  computed: {
    areaCode() {
      return this.$store.state.listAreaIndex;
    },
  },
  //监控data中的数据变化
  watch: {
    // areaCode(newVal, oldVal) {
    //   console.log("区域code有改动 -- chart1: [" + newVal + "]");
    //   this.init()
    // },
  },
  //方法集合
  methods: {
    // device点击事件
    chartDeviceClick(e) {
      console.log(e.dataIndex);
      console.log(this.devCodes[e.dataIndex]);
      // return
      this.$store.commit("setListTypeIndex", e.dataIndex+1);
      this.$store.commit("setListTotal", e.value);
      this.$store.commit("setListTitle", '在用设备数');
      this.$store.commit("setListCategory", "postGetUseDevList");
      let params = {
        routePath: "device-count",
      };
      this.$router.push({
        name: "list-page",
        params: params,
      });
    },
    init() {
      this.loadingFlag = true;
      let area_code = this.$store.getters.getAreaList[
        this.$store.getters.getListAreaIndex
      ]["area_code"];
      let params = { area_code };
      this.$http.postGetDevInfo(params).then((e) => {
        let ret = e.data;
        if (ret.errorCode != 0) {
          console.log("后台运行异常!");
          return console.log(ret);
        }
        // console.log('--- chart1 ---');
        // console.log(ret.data);
        let arrs = {
          types: [],
          data: [],
          count: ''
        }
        ret.data.devCharData.forEach(ele => {
          
          this.devCodes.push(ele.dev_code)

          arrs.types.push(ele.dev_name)

          if (ele.dev_code == '3000') {
             
            // console.log('电梯: ' + ele.dev_num);
            // arrs.data.push(Math.floor(ele.dev_num / 1000))
            
            if (ele.dev_num > 1000) {
              arrs.data.push(ele.dev_num / 1000)
              arrs.count = "千台"
            }else if(ele.dev_num > 100 && ele.dev_num < 1001){
              arrs.data.push(ele.dev_num / 100)
              arrs.count = "百台"
            }else{
              arrs.data.push(ele.dev_num)
              arrs.count = "台"
            }

            return
          }

          arrs.data.push(ele.dev_num)
           
        });

        this.$nextTick(() => {
          //   this.chart6(ret.data)
          this.indexDeviceData = getDeviceOption(arrs)
          this.loadingFlag = false
        });
      });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.init();
  },
};
</script>
<style lang='less' scoped>
/* @import url(); 引入css类 */
.chart1 {
  width: 100%;
  height: 100%;
  position: relative;
  .e-loading {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
  }
}
</style>